package com.zlp.partice;


import org.junit.Test;

import java.util.*;

/**
 * @Author: zhulaiping
 * @Date: 2021/10/20 9:49
 */
public class Day1020 {

    public int minMoves(int[] nums) {
        int sum = 0,min = Integer.MIN_VALUE;
        for (int num:nums) {
            sum += num;
            min = Math.min(min,num);
        }
        return sum-nums.length*min;
    }

    @Test
    public void ee(){
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        String[] array = list.stream().toArray(String[]::new);

        //System.out.println(array.length);
        //Arrays.stream(array).forEach(System.out::println);
        Integer[] integers = list.stream()
                .map(o -> Integer.valueOf(o))
                .toArray(Integer[]::new);
        for(int num:integers){
            System.out.println(num);
        }
    }

    public List<Integer> majorityElement(int[] nums) {
        Map<Integer,Integer>map = new HashMap<>();
        for (int num:nums){
            map.put(num,map.getOrDefault(num,0)+1);
        }
        List<Integer> ret = new ArrayList<>();
        for (int key:map.keySet()){
            if(map.get(key)>nums.length/3){
                ret.add(key);
            }
        }
        return ret;
    }

    public int[] constructRectangle(int area) {
        int[] ret = new int[2];
        for (int i = (int) Math.sqrt(area);i>=1;i++){
            if(area%i == 0){
                ret[0] = i;
                ret[1] = area/i;
            }
        }
        return ret;
    }

    public boolean searchMatrix(int[][] matrix, int target) {
        int n = matrix[0].length-1,m = 0;
        while (n>=0&&m<matrix.length){
            if(matrix[m][n] == target) return true;
            else if(matrix[m][n] < target) n--;
            else m++;
        }
        return false;
    }

    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        LinkedList<Integer> stack = new LinkedList<>();
        Map<Integer,Integer>map = new HashMap<>();
        int[] ret = new int[nums1.length];
        for (int num:nums2){
            while (stack.size() > 0 && stack.peek() < num) {
                Integer key = stack.pop();
                map.put(key, num);
            }
            stack.push(num);
        }
        for (int i = 0;i<nums1.length;i++){
            ret[i] = map.getOrDefault(nums1[i],-1);
        }
        return ret;
    }

}
